var error_msg = function (msg) {
    layui.use(['layer', 'form'], function(){
        var layer = layui.layer;
        layer.msg(msg,{
            icon: 5,
            time: 3000
        });
    });
};

var success_msg = function (msg) {
    layui.use(['layer', 'form'], function(){
        var layer = layui.layer;
        layer.msg(msg,{
            icon: 1,
            time: 3000
        });
    });
};

var check_phone = function (obj){
    var phone = obj.val();
    if (phone==null || phone=="") {
        error_msg("必填项不能为空");
        focus_addclass(obj);
        return false;
    }
    if(!(/^[1](([3][0-9])|([4][5,6,7,8,9])|([5][^4,6,9])|([6][6])|([7][3,4,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$/.test(phone))){
        error_msg("请输入正确的手机号");
        focus_addclass(obj);
        return false;
    }
    return true;
};

var check_email = function (obj) {
    var email = obj.val();
    if (email==null || email=="") {
        error_msg("必填项不能为空");
        focus_addclass(obj);
        return false;
    }
    if (!(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(email))) {
        error_msg("请输入正确的邮箱");
        focus_addclass(obj);
        return false;
    } else {
        return "email";
    }

};

var check_phone_email = function (obj){
    var phone_email = obj.val();
    if (phone_email==null || phone_email=="") {
        error_msg("必填项不能为空");
        focus_addclass(obj);
        return false;
    }

    //isNaN()判断是否为非数值
    if (!isNaN(phone_email)) {
        if(!(/^[1](([3][0-9])|([4][5,6,7,8,9])|([5][^4,6,9])|([6][6])|([7][3,4,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$/.test(phone_email))){
            error_msg("请输入正确的手机号");
            focus_addclass(obj);
            return false;
        }else {
            return "phone";
        }
    } else {
        if (!(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(phone_email))) {
            error_msg("请输入正确的邮箱");
            focus_addclass(obj);
            return false;
        } else {
            return "email";
        }
    }

    return true;
};

var check_val = function (obj) {
    var val = obj.val();
    if (val==null || val=="") {
        error_msg("必填项不能为空");
        focus_addclass(obj);
        return false;
    }
    return true;
};

var check_passwd = function (obj) {
    debugger;
    var val = obj.val();
    if (val==null || val=="") {
        error_msg("必填项不能为空");
        focus_addclass(obj);
        return false;
    }
    if (!(/^.{6,16}$/.test(val))) {
        error_msg("请输入6到16个字符");
        focus_addclass(obj);
        return false;
    }
    return true;
};

var focus_addclass = function (obj) {
    obj.focus();
    obj.addClass("layui-form-danger");
}
